package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mamba
 * @version 1.0
 * @Description
 * @date 2021/1/1 13:13
 * 索引管理
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//后来封装
    //@Autowired
    //private ElasticsearchTemplate elasticsearchTemplate;//早期封装
    @Autowired
    private ListService listService;

    //开始搜索 json串保存再请求体中
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){

        return listService.list(searchParam);
    }

    //mappings 映射
    @GetMapping("/mappings")
    public Result mappingES(){
        elasticsearchRestTemplate.createIndex(Goods.class);//创建库
        elasticsearchRestTemplate.putMapping(Goods.class);//映射 表 字段 指定字段类型
        return Result.ok();
    }


    //上架  商品sku 到ES索引库
    @GetMapping("onSale/{skuId}")
    public Result onsale(@PathVariable Long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架  从ES库存中删除索引
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }

    //更新热度
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore (@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();

    }






}
